package plugins.AI;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.ext.form.control.Markdown;

import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

public class DishesAnalysis extends AbstractBillPlugIn {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addItemClickListeners("tbmain");
    }

    public void itemClick(ItemClickEvent e) {
        super.itemClick(e);
        if (e.getItemKey().equalsIgnoreCase("dcb4_dish_anal")) {
            // 1. 收集菜品基本信息
            JSONObject dishInfo = new JSONObject();
            dishInfo.put("dishName", this.getModel().getValue("name").toString());
            dishInfo.put("dishPrice", this.getModel().getValue("dcb4_price").toString());
            dishInfo.put("dishMerchant", this.getModel().getValue("dcb4_merchant").toString());
            dishInfo.put("dishMaterial", this.getModel().getValue("dcb4_material").toString());

            // 2. 收集营养成分数据
            JSONArray nutritionArray = new JSONArray();
            DynamicObjectCollection nutritionFactsList = this.getModel().getEntryEntity("dcb4_nutritionfactslist");

            for (DynamicObject row : nutritionFactsList) {
                JSONObject nutrition = new JSONObject();
                nutrition.put("project", row.getString("dcb4_project"));
                nutrition.put("contain", row.getString("dcb4_contain"));
                nutrition.put("nvr", row.getString("dcb4_nvr"));
                nutritionArray.add(nutrition);
            }
            dishInfo.put("nutritionFacts", nutritionArray);

            // 3. 调用AI生成文本分析
            Map<String, String> variableMap = new HashMap<>();
            variableMap.put("dishInfo", dishInfo.toJSONString());

            Object[] textParams = new Object[]{
                    getPromptFid("prompt-250714C81B81EB"), // 文本分析提示词
                    "开始分析这道菜",
                    variableMap
            };

            Map<String, Object> textResult = DispatchServiceHelper.invokeBizService(
                    "ai", "gai", "GaiPromptService", "syncCall", textParams
            );

            JSONObject textResultJson = new JSONObject(textResult);
            JSONObject textData = textResultJson.getJSONObject("data");
            this.getModel().setValue("dcb4_txt", textData.getString("llmValue"));

            this.getView().setVisible(true, "dcb4_advconap1");
            Markdown mk = this.getView().getControl("dcb4_md");
            mk.setText(textData.getString("llmValue"));
        }
    }

    public long getPromptFid(String billNo) {
        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(
                "gai_prompt",
                "number,id",
                new QFilter[]{new QFilter("number", QCP.equals, billNo)}
        );
        return dynamicObject.getLong("id");
    }


    @Override
    public void afterBindData(EventObject eventObject) {
        Markdown mk = this.getView().getControl("dcb4_md");
        Object txtValue = this.getModel().getValue("dcb4_txt");
        if (txtValue != null) {
            mk.setText(txtValue.toString());
        }
    }

}
